// 引入阿里云短信的配置
const Core = require("@alicloud/pop-core");
const aliconfig = require("../util/aliconfig");
let client = new Core(aliconfig.alicloud);
var requestOption = {
    method: "POST",
};
function rand(min, max) {
    return Math.floor(Math.random() * (max - min)) + min;
}
let validate = [];
sendCoreCode = (req, res) => {
    let phone = req.query.phone;
    let randomcode = rand(1000, 9999);
    // 这就是要发送的配置，里面的number是接收验证码的号码，TemplateParam为发送的验证码，其他的为自身的配置
    var params = {
        RegionId: "cn-hangzhou",
        PhoneNumbers: phone,
        SignName: "chengsong商城",
        TemplateCode: "SMS_205391612",
        TemplateParam: JSON.stringify({ code: randomcode }),
    };
    client.request("SendSms", params, requestOption).then(
        (result) => {
            console.log(JSON.stringify(result));
            if (result) {
                res.send({
                    code: 200,
                    yourcode: randomcode,
                    msg: "发送成功",
                });
                validate.push({
                    phone: phone,
                    code: randomcode,
                });
            } else {
                res.send({
                    code: 400,
                    msg: "发送失败",
                });
            }
        },
        (ex) => {
            console.log(ex);
        }
    );
};

module.exports = { sendCoreCode };
